import dayjs from "dayjs";
import { getSessionPage, logoff } from "@/api/vap/cluster";
import { type PaginationProps } from "@pureadmin/table";
import { reactive, ref, computed, onMounted } from "vue";
import { useVaStoreHook } from "@/store/modules/va";

const vaStore = useVaStoreHook();

export function useSession() {
  const form = reactive({
    pageNum: 1,
    pageSize: 10,
    hostIp: "",
    status: "",
    startTime: "",
    endTime: ""
  });
  const dataList = ref([]);
  const loading = ref(true);
  const pagination = reactive<PaginationProps>({
    total: 0,
    pageSize: 10,
    currentPage: 1,
    background: true
  });
  const columns: TableColumnList = [
    {
      type: "selection",
      width: 55,
      align: "left",
      hide: ({ checkList }) => !checkList.includes("勾选列")
    },
    {
      label: "序号",
      type: "index",
      width: 70,
      hide: ({ checkList }) => !checkList.includes("序号列")
    },
    {
      label: "会话ID",
      prop: "id",
      minWidth: 130
    },
    {
      label: "应用服务器IP",
      prop: "hostIp",
      minWidth: 130
    },
    {
      label: "应用服务器端口",
      prop: "connectPort",
      minWidth: 130
    },
    {
      label: "win会话ID",
      prop: "winSessionId",
      minWidth: 130
    },
    {
      label: "画面尺寸",
      minWidth: 90,
      cellRenderer: ({ row }) => (
        <el-tag>
          {row.width} x {row.height}
        </el-tag>
      )
    },
    {
      label: "会话状态",
      prop: "status",
      minWidth: 130,
      formatter: ({ status }) => {
        console.log(status);
        let statusContext = "未知状态";
        switch (status) {
          case 1:
            statusContext = "握持中";
            break;
          case 2:
            statusContext = "握持结束";
            break;
          case 3:
            statusContext = "未开始";
            break;
          default:
            statusContext = "未知状态";
            break;
        }
        return statusContext;
      }
    },
    {
      label: "连接信息",
      prop: "message",
      minWidth: 130
    },
    {
      label: "创建时间",
      minWidth: 90,
      prop: "createdAt",
      formatter: ({ createdAt }) => {
        console.log(createdAt);
        return dayjs(Number(createdAt) * 1000).format("YYYY-MM-DD HH:mm:ss");
      }
    },
    {
      label: "操作",
      fixed: "right",
      minWidth: 200,
      slot: "operation"
    }
  ];
  const buttonClass = computed(() => {
    return [
      "!h-[20px]",
      "reset-margin",
      "!text-gray-500",
      "dark:!text-white",
      "dark:hover:!text-primary"
    ];
  });
  function clickLogoffSession(row) {
    console.log(row);
    logoff(row.id)
      .then()
      .catch(e => {
        ElMessage.error(e.response.data.msg);
      });
    onSearch();
  }
  function clickBreakSession(row) {
    console.log(row);
    // breakSession(row.hostSessionId).catch(e => {
    //   ElMessage.error(e.response.data.msg);
    // });
    onSearch();
  }
  function clickSendInstruct(row) {
    console.log(row);
    vaStore.instructRowSessionId = row.id;
    vaStore.sendInstructDialog = true;
  }

  function handleSizeChange(val: number) {
    console.log(`${val} items per page`);
  }

  function handleCurrentChange(val: number) {
    console.log(`current page: ${val}`);
  }

  function handleSelectionChange(val) {
    console.log("handleSelectionChange", val);
  }
  function clickTakeBtn(val) {
    console.log("clickTakeBtn", val);
  }
  function clickMonitorBtn(val) {
    console.log("clickMonitorBtn", val);
    window.open(window.location.origin + "#/monitor?id=" + val.id);
  }
  function clickRecoverBtn(val) {
    console.log(val);
    window.open(
      window.location.origin +
        "#/desktop?id=" +
        val.id +
        "&isReuse=true" +
        "&isOpen=false" +
        "&appId=0"
    );
  }

  async function onSearch() {
    loading.value = true;
    const { list, total } = await getSessionPage(form);
    console.log(list);
    dataList.value = list;
    pagination.total = total;
    setTimeout(() => {
      loading.value = false;
    }, 500);
  }

  const resetForm = formEl => {
    if (!formEl) return;
    formEl.resetFields();
    onSearch();
  };

  onMounted(() => {
    onSearch();
  });

  return {
    form,
    loading,
    columns,
    dataList,
    pagination,
    buttonClass,
    onSearch,
    resetForm,
    clickLogoffSession,
    clickBreakSession,
    clickSendInstruct,
    clickMonitorBtn,
    clickRecoverBtn,
    clickTakeBtn,
    handleSizeChange,
    handleCurrentChange,
    handleSelectionChange
  };
}
